import _ from 'lodash'

interface Action  {
    type:string,
    payload:any
}

export interface InitialState {
    loginData:any,
    isCollapsed:boolean,
    keyPath:string[]
}

var initialState = {
    loginData:{},
    isCollapsed:false,
    keyPath:[]
}

//定义reducer纯函数
export function reducer(state:InitialState = initialState,action:Action){
    //深拷贝
    var newstate = _.cloneDeep(state);

    //判断action
    if( action.type == 'SAVE_LOGINDATA' ){
        newstate.loginData = action.payload;
        return newstate;
    }else if( action.type == 'REMOVE_LOGINDATA' ){
        newstate.loginData = {};
        return newstate;
    }else if( action.type == 'CHANGE_COLLAPSE' ){
        newstate.isCollapsed = !newstate.isCollapsed;
        return newstate;
    }else if( action.type == 'SAVE_KEYPATH' ){
        newstate.keyPath = action.payload;
        return newstate;
    }else{
        return state;
    }
}